package org.dromara.company.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.admin.domain.bo.OrderProductBo;
import org.dromara.admin.domain.vo.OrderProductVo;
import org.dromara.admin.service.IOrderProductService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司端订单产品明细关联
 *
 * @author Lion Li
 * @date 2025-10-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/company/orderProduct")
public class OrderProductController extends BaseController {

    private final IOrderProductService orderProductService;

    /**
     * 查询订单产品明细关联列表
     */
//    @SaCheckPermission("system:orderProduct:list")
    @GetMapping("/list")
    public TableDataInfo<OrderProductVo> list(OrderProductBo bo, PageQuery pageQuery) {
        return orderProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单产品明细关联列表
     */
//    @SaCheckPermission("system:orderProduct:export")
    @Log(title = "订单产品明细关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OrderProductBo bo, HttpServletResponse response) {
        List<OrderProductVo> list = orderProductService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单产品明细关联", OrderProductVo.class, response);
    }

    /**
     * 获取订单产品明细关联详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:orderProduct:query")
    @GetMapping("/{id}")
    public R<OrderProductVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(orderProductService.queryById(id));
    }

    /**
     * 新增订单产品明细关联
     */
//    @SaCheckPermission("system:orderProduct:add")
    @Log(title = "订单产品明细关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OrderProductBo bo) {
        return toAjax(orderProductService.insertByBo(bo));
    }

    /**
     * 修改订单产品明细关联
     */
//    @SaCheckPermission("system:orderProduct:edit")
    @Log(title = "订单产品明细关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OrderProductBo bo) {
        return toAjax(orderProductService.updateByBo(bo));
    }

    /**
     * 删除订单产品明细关联
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:orderProduct:remove")
    @Log(title = "订单产品明细关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(orderProductService.deleteWithValidByIds(List.of(ids), true));
    }
}
